package com.test3;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.util.concurrent.CompletableFuture;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileAppender;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.io.file.Tailer;
import cn.hutool.core.io.watch.WatchMonitor;
import cn.hutool.core.io.watch.Watcher;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;

public class FileTailer {
	
	private static final String name = "test.txt";
	
	private static final String path = "C:/Users/domains90/Desktop/";
	
	private static final String file = path+name;
	
	
	public static void main(String[] args) throws InterruptedException {
//		CompletableFuture.runAsync(()->{
//			File file = FileUtil.file(path);
//			//这里只监听文件或目录的修改事件
//			WatchMonitor watchMonitor = WatchMonitor.create(file, WatchMonitor.ENTRY_MODIFY);
//			watchMonitor.setWatcher(new Watcher(){
//			    @Override
//			    public void onCreate(WatchEvent<?> event, Path currentPath) {
//			        Object obj = event.context();
//			        Console.log("创建：{}-> {}", currentPath, obj);
//			    }
//
//			    @Override
//			    public void onModify(WatchEvent<?> event, Path currentPath) {
//			        Object obj = event.context();
//			        Console.log("修改：{}-> {}", currentPath, obj);
//			    }
//
//			    @Override
//			    public void onDelete(WatchEvent<?> event, Path currentPath) {
//			        Object obj = event.context();
//			        Console.log("删除：{}-> {}", currentPath, obj);
//			    }
//
//			    @Override
//			    public void onOverflow(WatchEvent<?> event, Path currentPath) {
//			        Object obj = event.context();
//			        Console.log("Overflow：{}-> {}", currentPath, obj);
//			    }
//			});
//
//			//设置监听目录的最大深入，目录层级大于制定层级的变更将不被监听，默认只监听当前层级目录
//			watchMonitor.setMaxDepth(3);
//			//启动监听
//			watchMonitor.start();
//		},Executor.get());
		
		Tailer tailer = new Tailer(FileUtil.file(file), Tailer.CONSOLE_HANDLER, 2);
		tailer.start();
		
		Thread.sleep(1000);
		wirte();	
	}
	
	public static void wirte() {
		FileWriter writer = new FileWriter(file);
		writer.write("ceshi");
		
		
		FileAppender appender = new FileAppender(new File(file), 16, true);
		appender.append("123");
		appender.append("abc");
		appender.append("xyz");
		appender.flush();

		StrUtil.format("aa{}","as");
		
	}
	
	

}
